"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.qiniu = void 0;
const axios_1 = __importDefault(require("axios"));
const log4js_1 = require("log4js");
const request_1 = __importDefault(require("request"));
const qin = require('node-qiniu');
const logger = (0, log4js_1.getLogger)('qiniu');
class qiniu {
    constructor(access, secret, bucket) {
        this.access = access;
        this.secret = secret;
        this.bucket = bucket;
        qin.config({
            access_key: this.access,
            secret_key: this.secret
        });
    }
    upload(url, name) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const { status } = yield axios_1.default.get(url);
            }
            catch (error) {
                return;
            }
            console.log(url);
            try {
                const imagesBucket = qin.bucket(this.bucket);
                const puttingStream = imagesBucket.createPutStream(`${name}.png`);
                request_1.default.get(url).pipe(puttingStream);
                return `http://r4auhvjfb.hd-bkt.clouddn.com/${name}.png`;
            }
            catch (error) {
                logger.error(`qiniu load ${error.message}`);
            }
            return `http://r4auhvjfb.hd-bkt.clouddn.com/error.png`;
        });
    }
}
exports.qiniu = qiniu;
